/**
 * 格式化时间
 * @param {number} time - 时间戳
 * @param {string} format - 格式化
 * @return {string} - 格式化后的时间
 */
export const formatDateTime = (time, format = "YYYY-MM-DD HH:mm:ss") => {
    if (!time) return "-";
  
    const isSecondLevel = time.toString().length === 10;
    const date = new Date(isSecondLevel ? time * 1000 : time);
  
    const map = {
      YYYY: date.getFullYear(),
      MM: ("0" + (date.getMonth() + 1)).slice(-2),
      DD: ("0" + date.getDate()).slice(-2),
      HH: ("0" + date.getHours()).slice(-2),
      mm: ("0" + date.getMinutes()).slice(-2),
      ss: ("0" + date.getSeconds()).slice(-2),
    };
  
    return format.replace(/YYYY|MM|DD|HH|mm|ss/g, (matched) => map[matched]);
  }

// 将时间戳转为 2024-12-25T12:00:00Z 格式
export const formatDateToISOString = (timestamp) => {
  const date = new Date(timestamp);
  const isoString = date.toISOString();
  const formattedDate = isoString.split('.')[0] + 'Z';
  return formattedDate;
}

// 获取当前时间 格式为 yyyy-MM-dd HH:mm:ss
export const formattedCurrentTime = () => {
  const date = new Date();
  // 获取年份、月份、日期、小时、分钟、秒
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');
  // 拼接结果
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}